Skip to content

feat(redis): implement redis keys reset - #952

Open
tibo-pdn wants to merge 25 commits into
mainfrom
704-reset-all-redis-keys-when-api-startup
Open

feat(redis): implement redis keys reset#952
tibo-pdn wants to merge 25 commits into
mainfrom
704-reset-all-redis-keys-when-api-startup

Conversation

@tibo-pdn

@tibo-pdn tibo-pdn commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Overview

Resolves #704.

Redis keys were not cleared when the API started. The only startup-time reset was Limiter.reset()
(called in the lifespan), which delegates to the limits library's redis_storage.reset() — and that
only deletes keys prefixed with LIMITS (rate limiting). All the other keys written by the app
(ogl_mg:* inflight gauges, ogl_ts:* latency time-series) survived a restart.

This PR flushes the whole Redis logical DB on API startup, once per process, from the gunicorn
master (on_starting hook) before workers are forked. This placement is deliberate: with several
workers in production, doing the flush in the per-worker lifespan would mean that a single crashed
worker being respawned by gunicorn would wipe the Redis state (rate limits, inflight gauges, metrics)
still in use by its live sibling workers — including driving the inflight gauges negative. Running it
once in the master avoids that.

Changes:

  • Add reset_redis_keys(url) in api/utils/redis.py — a synchronous flushdb (the gunicorn master
    runs outside the event loop).

  • Call it from on_starting(server) in scripts/gunicorn.conf.py.

  • Remove the now-redundant Limiter.reset() method and its lifespan call (superseded by the full flush),
    and drop the associated obsolete unit tests.

  • All Redis keys are flushed when the API starts

  • The flush runs once per process (gunicorn master), so a crashed-worker respawn does not wipe the
    shared Redis state of live workers

  • The obsolete Limiter.reset() (partial, LIMITS-only reset) is removed

Breaking changes:

  • No breaking changes
  • This PR contains breaking changes (explain below)

Note: behavior change (not an API break) — in local make dev the API runs under uvicorn (no gunicorn
master), so the startup flush no longer happens in dev. Production runs under gunicorn (startup_api.sh),
where the hook applies.

Check lists

Review checklist

  • Updated or added documentation — n/a, internal startup behavior, no user-facing docs impacted.
  • Updated or added unit tests — no unit test added for reset_redis_keys; the obsolete
    Limiter.reset() unit tests were removed along with the method.
  • Updated or added integration tests — none added for the new startup flush behavior.
  • No debug logs or commented-out code left — ⚠️ api/utils/lifespan.py still contains a commented-out
    # await client.flushdb() line to remove before merge.
  • No secrets or environment variables committed in clear text
  • Code is linted and formatted using the project pre-commit hooks — to confirm by the author.

If api/sql/models.py has been modified, please confirm that the following steps have been completed:

  • Alembic migration has been generated
  • Alembic migration upgrade has been tested locally
  • Alembic migration downgrade has been tested locally

api/sql/models.py was not modified — not applicable.

Deployment checklist

For each of the following items, please confirm if the PR concerns the deployment of the changes:

  • Alembic migration has been generated
  • Configuration file has been modified
  • Environment variables have been modified

None of the above apply: no schema change, no config change, no new/updated environment variables.
The flush reuses the existing dependencies.redis.url configuration.

Additional Notes

Points worth a reviewer's attention:

  • Placement rationale (main design point): the flush is in the gunicorn master on_starting hook
    rather than the per-worker lifespan, specifically to avoid a respawned worker wiping the shared Redis
    state of live sibling workers (rate-limit counters, ogl_mg:* inflight gauges — decrement_inflight
    has no floor at 0, so a mid-life flush could push gauges negative and skew the "least busy" load
    balancer — and ogl_ts:* metrics).
  • Multi-replica / rolling deploy: each new container still flushes the shared Redis on its own boot.
    A flush-on-start over a Redis shared across replicas is inherently in tension there; out of scope for
    this PR, which targets the frequent and dangerous case (worker respawn).
  • Celery: when the (deprecated) Celery dependency is enabled, its broker/result-backend default to the
    same Redis URL/DB, so the startup flush also drops any queued Celery tasks/results.
  • Leftover to clean up: remove the commented # await client.flushdb() in api/utils/lifespan.py.
  • Test coverage gap: consider adding a test for reset_redis_keys (seed keys → call → assert
    DBSIZE == 0) since the previous startup-reset unit tests were removed.

@tibo-pdn tibo-pdn self-assigned this Jul 6, 2026
@tibo-pdn
tibo-pdn requested a review from leoguillaume July 10, 2026 15:40
@tibo-pdn

Copy link
Copy Markdown
Contributor Author

@copilot resolve the merge conflicts in this pull request

@tibo-pdn
tibo-pdn marked this pull request as ready for review July 10, 2026 15:40

Copilot AI commented Jul 10, 2026

Copy link
Copy Markdown

@copilot resolve the merge conflicts in this pull request

Resolved the merge conflicts and updated the branch by merging origin/main into this PR in commit ebc12d08.

leoguillaume and others added 16 commits July 11, 2026 11:40
* fix: semgrep full

* fix: release update
Co-authored-by: leoguillaume <62661249+leoguillaume@users.noreply.github.com>
Bumps [undici](https://github.com/nodejs/undici) from 7.25.0 to 7.28.0.
- [Release notes](https://github.com/nodejs/undici/releases)
- [Commits](nodejs/undici@v7.25.0...v7.28.0)

---
updated-dependencies:
- dependency-name: undici
  dependency-version: 7.28.0
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Léo Guillaume <62661249+leoguillaume@users.noreply.github.com>
* refacto(keys): migrate GET /v1/admin/tokens to clean architecture as /v1/admin/keys.

Add paginated list and get-by-id endpoints with use cases, repository, and tests; remove legacy GET handlers.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Update unit coverage badge

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: leoguillaume <leoguillaume@users.noreply.github.com>
Bumps [tar](https://github.com/isaacs/node-tar) from 7.5.13 to 7.5.21.
- [Release notes](https://github.com/isaacs/node-tar/releases)
- [Changelog](https://github.com/isaacs/node-tar/blob/main/CHANGELOG.md)
- [Commits](isaacs/node-tar@v7.5.13...v7.5.21)

---
updated-dependencies:
- dependency-name: tar
  dependency-version: 7.5.21
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps  and [svgo](https://github.com/svg/svgo). These dependencies needed to be updated together.

Updates `svgo` from 4.0.1 to 4.0.2
- [Release notes](https://github.com/svg/svgo/releases)
- [Commits](svg/svgo@v4.0.1...v4.0.2)

Updates `svgo` from 3.3.3 to 3.3.4
- [Release notes](https://github.com/svg/svgo/releases)
- [Commits](svg/svgo@v4.0.1...v4.0.2)

---
updated-dependencies:
- dependency-name: svgo
  dependency-version: 4.0.2
  dependency-type: indirect
- dependency-name: svgo
  dependency-version: 3.3.4
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
#968)

* fix(cicd): install missing packages to run e2e tests and change documentation

* Update unit coverage badge

---------

Co-authored-by: leoguillaume <leoguillaume@users.noreply.github.com>
Bumps [sharp](https://github.com/lovell/sharp) from 0.34.5 to 0.35.0.
- [Release notes](https://github.com/lovell/sharp/releases)
- [Commits](lovell/sharp@v0.34.5...v0.35.0)

---
updated-dependencies:
- dependency-name: sharp
  dependency-version: 0.35.0
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…numeration (#963)

* Added generic invalid credentials HTTP exception

* Invalid password or user not found to raise new invalid creds exception

* Fixed invalid creds integration tests

* Removed unused InvalidPasswordHTTPException

* Update unit coverage badge

* Update unit coverage badge

---------

Co-authored-by: Bakr Annour <dinum-304418@MacBook-Pro-de-DINUM-304418.local>
Co-authored-by: bakr-a <bakr-a@users.noreply.github.com>
…ure (#962)

* Refacto patch user

* fix expires in users fastapi schemas

* WIP: refacto update behavior

* prevent password from being returned by fastapi

* Update unit coverage badge

* Update unit coverage badge

---------

Co-authored-by: Benjamin PILIA <benjamin.pilia@protonmail.com>
Co-authored-by: benjaminpilia <benjaminpilia@users.noreply.github.com>
Co-authored-by: Léo Guillaume <62661249+leoguillaume@users.noreply.github.com>
Co-authored-by: leoguillaume <leoguillaume@users.noreply.github.com>
…m:etalab-ia/OpenGateLLM into 704-reset-all-redis-keys-when-api-startup
tibo-pdn and others added 2 commits July 22, 2026 13:50
…m:etalab-ia/OpenGateLLM into 704-reset-all-redis-keys-when-api-startup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[depracted] Remove old rerank signature

5 participants